package com.wzc.bigevent.service.impl;

import com.wzc.bigevent.mapper.ArticleCategoryMapper;
import com.wzc.bigevent.pojo.Category;
import com.wzc.bigevent.pojo.Result;
import com.wzc.bigevent.pojo.User;
import com.wzc.bigevent.service.ArticleCategoryService;
import com.wzc.bigevent.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleCategoryServiceImpl implements ArticleCategoryService {

    @Resource
    private ArticleCategoryMapper articleCategoryMapper;

    /**
     * 添加文章分类
     * @param category
     */
    @Override
    public void addCategory(Category category) {
        // 获取当前登录用户信息
        Map<String, Object> userMap = ThreadLocalUtil.get();
        Integer userId = (Integer) userMap.get("id");

        // 封装数据
        category.setCreateUser(userId);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        // 添加文章分类
        articleCategoryMapper.add(category);
    }


    /**
     * 获取文章分类列表
     * @return
     */
    @Override
    public Result<List<Category>> getCategoryList() {
        // 获取当前登录用户信息
        Map<String, Object> userMap = ThreadLocalUtil.get();
        Integer userId = (Integer) userMap.get("id");

        // 获取文章分类列表
        List<Category> categoryList = articleCategoryMapper.getCategoryListByUserId(userId);
        return Result.success(categoryList);
    }


    /**
     * 根据id获取文章分类详情
     * @param id
     * @return
     */
    @Override
    public Result<Category> getCategoryDetail(Integer id) {
        // 获取文章分类详情
        Category category = articleCategoryMapper.getCategoryDetailById(id);
        return Result.success(category);
    }


    /**
     * 更新文章分类
     * @param category
     */
    @Override
    public void updateCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        articleCategoryMapper.update(category);
    }


    /**
     * 删除文章分类
     * @param id
     */
    @Override
    public Boolean deleteCategory(Integer id) {
        return articleCategoryMapper.deleteById(id);
    }
}
